package web.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.quartz.CronExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import kit.StringUtil;
import plugin.route.ControllerBind;
import web.common.BaseController;
import web.common.quartz.ScheduleJob;
import web.common.quartz.ScheduleJobUtils;

@ControllerBind(controllerKey = "/admin/task")
public class TaskController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(TaskController.class);
	private ScheduleJobUtils scheduleJobUtils = ScheduleJobUtils.me;

	/**
	 * 默认页面
	 * 
	 * @return
	 */
	public String list() {
		List<ScheduleJob> scheduleJobs = scheduleJobUtils.getAllScheduleJob();
		setAttr("scheduleJobs", scheduleJobs);
		return "task/list";
	}

	/**
	 * 获取正在运行的定时任务
	 */
	public String getAllJobsRun() {
		List<ScheduleJob> scheduleJobs = scheduleJobUtils.getAllRuningScheduleJob();
		setAttr("scheduleJobs", scheduleJobs);
		return "task/list";
	}


	/**
	 * 添加
	 * 
	 * @param user
	 * @param model
	 */
	public void create() {
		ScheduleJob scheduleJob = new ScheduleJob();
		scheduleJob.setName(getPara("name"));
		scheduleJob.setGroup(getPara("group"));
		scheduleJob.setCronExpression(getPara("cronExpression"));
		scheduleJob.setClassName(getPara("className"));
		scheduleJob.setStatus("1");
		scheduleJobUtils.add(scheduleJob);
		sendAlert("添加成功", "/admin/task/list");
	}

	/**
	 * 删除任务
	 */
	public void delete() {
		String name = getPara("name");
		String group = getPara("group");
		scheduleJobUtils.delJob(name, group);
		sendAlert("删除成功", "/admin/task/list");
	}

	/**
	 * 修改表达式
	 */
	public void update() {
		String name = getPara("name");
		String group = getPara("group");
		String cronExpression = getPara("cronExpression");
		// 验证cron表达式
		if (CronExpression.isValidExpression(cronExpression)) {
			scheduleJobUtils.modifyTrigger(name, group, cronExpression);
			renderJson(true);
		} else {
			renderJson(false);
		}
	}

	/**
	 * 立即运行一次
	 */
	public void startNow() {
		String name = getPara("name");
		String group = getPara("group");
		scheduleJobUtils.startNowJob(name, group);
		sendAlert("执行成功", "/admin/task/list");
	}

	/**
	 * 暂停任务
	 */
	public void updateStatus() {
		String name = getPara("name");
		String group = getPara("group");
		String tostatus = getPara("tostatus");
		if (StringUtil.equalsIgnoreCase("stop", tostatus)) {
			scheduleJobUtils.stopJob(name, group);
			sendAlert("已暂停任务", "/admin/task/list");
			return;
		} else if (StringUtil.equalsIgnoreCase("resume", tostatus)) {
			scheduleJobUtils.restartJob(name, group);
			sendAlert("执行成功", "/admin/task/list");
			return;
		}
		
	}


	/**
	 * 获取所有trigger
	 */
	public void getTriggers(HttpServletRequest request) {
		List<ScheduleJob> scheduleJobs = scheduleJobUtils.getTriggersInfo();
		System.out.println(scheduleJobs.size());
		request.setAttribute("triggers", scheduleJobs);
	}

	/**
	 * cron表达式生成页
	 */
	public String quartzCronCreate() {
		return "system/quartzCron";
	}
}
